/*
    File:       OSMutex.h

    Contains:   Platform - independent mutex header. The implementation of this object
                is platform - specific. Each platform must define an independent
                OSMutex.h & OSMutex.cpp file.
                
                This file is for Mac OS X Server only

    

*/

#ifndef _OSMUTEX_H_
#define _OSMUTEX_H_

#include <stdlib.h>

#ifndef __Win32__
#include <sys/errno.h>
#include <pthread.h>
#endif

#include "OSHeaders.h"
#include "MyAssert.h"

class OSCond;

class OSMutex
{
    public:

        OSMutex();
        ~OSMutex();

        void Lock();
        void Unlock();
        
        // Returns true on successful grab of the lock, false on failure
        Bool TryLock();
	
	THREAD_ID GetFirstLockThreadID() {return m_FirstLockThreadID;}

    private:
#ifdef __Win32__
	CRITICAL_SECTION fMutex;
                
#else
        pthread_mutex_t fMutex;
#endif

	THREAD_ID	m_FirstLockThreadID;	
        friend class OSCond;
};

class   OSMutexLocker
{
    public:

        OSMutexLocker(OSMutex *inMutexP) : fMutex(inMutexP) { if (fMutex != NULL) fMutex->Lock(); }
        ~OSMutexLocker() {  if (fMutex != NULL) fMutex->Unlock(); }
        
        void Lock()         { if (fMutex != NULL) fMutex->Lock(); }
        void Unlock()       { if (fMutex != NULL) fMutex->Unlock(); }
        
    private:

        OSMutex*    fMutex;
};


#endif //_OSMUTEX_H_
